﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.CustomerManagement
{
    public partial class EditCustQuoteQuery : BasePage
    {
        private ICustQuoteBLL quoteBLL = IocContainer.Instance.Resolve<ICustQuoteBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!IsPostBack)
            {
                this.txtDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");                
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int quoteId = 0;
                    int.TryParse(Request.QueryString["QuoteId"], out quoteId);
                    if (quoteId > 0)
                    {
                        DataTable dt = quoteBLL.Query(string.Format(" and tcq.FItemId={0}", quoteId));
                        if (dt.Rows.Count > 0)
                        {
                            DataRow dr = dt.Rows[0];
                            this.txtQuoteCode.Text = dr["FNumber"].ToString();
                            txtBussId.Value = dr["FBusinessoppId"].ToString();
                            txtBussCode.Text = dr["BssCode"].ToString();
                            //txtEmpId.Value = dr["FMaker"].ToString();
                            //txtMaker.Text = dr["EmpName"].ToString();
                            txtDate.Text = dr["FDate"].ToString() == "" ? "" : DateTime.Parse(dr["FDate"].ToString()).ToString("yyyy-MM-dd");
                            txtProductName.Text = dr["ProductName"].ToString();
                            hidProductId.Value = dr["FProductId"].ToString();
                            txtVolume.Text = dr["FVolume"].ToString();
                            txtPrice.Text = dr["FPrice"].ToString();
                            txtTax.Text = dr["FTax"].ToString();
                            txtQty.Text = dr["FQTY"].ToString();
                            txtTaxPrice.Text = dr["FTaxPrice"].ToString();
                            txtUnit.Text = dr["FUnit"].ToString();
                            txtQtyVolume.Text = dr["FQTYVolume"].ToString();
                            txtAmount.Text = dr["FAmount"].ToString();
                            txtFaxAmount.Text = dr["FTaxAmount"].ToString();
                            txtNote.Text = dr["FNote"].ToString();
                        }
                    }
                }
                else
                {
                    // 销售线索过来的新增
                    if (Request.QueryString["LeadId"] != null && Request.QueryString["LeadCode"] != null)
                    {
                        this.txtBussId.Value = Request.QueryString["LeadId"];
                        this.txtBussCode.Text = Request.QueryString["LeadCode"];
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                CustQuote model = new CustQuote();
                model.FBusinessoppId =Int32.Parse(txtBussId.Value.Trim());
                //model.FMaker = Int32.Parse(txtEmpId.Value.Trim());
                model.FMaker = Int32.Parse(base.UserID);
                model.FDate = DateTime.Parse(txtDate.Text.Trim());
                model.FProductId = Int32.Parse(hidProductId.Value.Trim());
                model.FVolume = txtVolume.Text.Trim() == "" ? 0 : decimal.Parse(txtVolume.Text.Trim());
                model.FPrice = txtPrice.Text.Trim() == "" ? 0 : decimal.Parse(txtPrice.Text.Trim());
                model.FTax = txtTax.Text.Trim() == "" ? 0 : decimal.Parse(txtTax.Text.Trim());
                model.FQTY = txtQty.Text.Trim() == "" ? 0 : Int32.Parse(txtQty.Text.Trim());
                model.FTaxPrice = txtTaxPrice.Text.Trim() == "" ? 0 : decimal.Parse(txtTaxPrice.Text.Trim());
                model.FUnit = txtUnit.Text.Trim();
                model.FQTYVolume = txtQtyVolume.Text.Trim() == "" ? 0 : decimal.Parse(txtQtyVolume.Text.Trim());
                model.FAmount = txtAmount.Text.Trim() == "" ? 0 : decimal.Parse(txtAmount.Text.Trim());
                model.FTaxAmount = txtFaxAmount.Text == "" ? 0 : decimal.Parse(txtAmount.Text.Trim());
                model.FNote = this.txtNote.Text.Trim();
                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    quoteBLL.Add("Quo",model);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int quoteId = 0;
                    int.TryParse(Request.QueryString["QuoteId"], out quoteId);
                    model.FItemId = quoteId;
                    quoteBLL.Update(model);
                    isOK = true;
                }
                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }
    }
}